{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import random\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# to show gray image to show image matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_gray = cv2.imread('Lenna.png', 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[140 133 128 ... 222 218 213]\n",
      " [140 133 128 ... 222 218 213]\n",
      " [140 133 128 ... 222 218 213]\n",
      " ...\n",
      " [141 129 131 ... 128 129 129]\n",
      " [129 115 111 ... 126 130 129]\n",
      " [127 107 103 ... 135 132 132]]\n"
     ]
    }
   ],
   "source": [
    "cv2.imshow('Lenna',img_gray)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n",
    "print(img_gray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "uint8\n"
     ]
    }
   ],
   "source": [
    "# to show image data type\n",
    "print(img_gray.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(509L, 359L)\n"
     ]
    }
   ],
   "source": [
    "# to show gray image shape\n",
    "print(img_gray.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# to show origin image\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread('Lenna.png')\n",
    "cv2.imshow('Lenna',img)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[103  98 203]\n",
      "  [ 91  82 202]\n",
      "  [ 98  86 187]\n",
      "  ...\n",
      "  [162 215 252]\n",
      "  [156 210 249]\n",
      "  [149 204 247]]\n",
      "\n",
      " [[103  98 203]\n",
      "  [ 91  82 202]\n",
      "  [ 98  86 187]\n",
      "  ...\n",
      "  [162 215 252]\n",
      "  [156 210 249]\n",
      "  [149 204 247]]\n",
      "\n",
      " [[103  98 203]\n",
      "  [ 91  82 202]\n",
      "  [ 98  86 187]\n",
      "  ...\n",
      "  [162 215 252]\n",
      "  [156 210 249]\n",
      "  [149 204 247]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[ 88 101 204]\n",
      "  [112  92 183]\n",
      "  [110 102 178]\n",
      "  ...\n",
      "  [ 83  84 191]\n",
      "  [ 81  84 194]\n",
      "  [ 82  86 192]]\n",
      "\n",
      " [[ 87  90 188]\n",
      "  [105  73 169]\n",
      "  [ 95  78 158]\n",
      "  ...\n",
      "  [ 80  81 191]\n",
      "  [ 83  89 191]\n",
      "  [ 84  85 193]]\n",
      "\n",
      " [[ 85  85 187]\n",
      "  [ 99  72 155]\n",
      "  [ 95  72 145]\n",
      "  ...\n",
      "  [ 85  93 199]\n",
      "  [ 87  89 195]\n",
      "  [ 82  87 197]]]\n"
     ]
    }
   ],
   "source": [
    "# to show color image to show channels\n",
    "print(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(509L, 359L, 3L)\n"
     ]
    }
   ],
   "source": [
    "print(img.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#img crop\n",
    "img_crop = img[0:100, 0:200]\n",
    "cv2.imshow('img_crop', img_crop)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[103  91  98 ... 162 156 149]\n",
      " [103  91  98 ... 162 156 149]\n",
      " [103  91  98 ... 162 156 149]\n",
      " ...\n",
      " [ 88 112 110 ...  83  81  82]\n",
      " [ 87 105  95 ...  80  83  84]\n",
      " [ 85  99  95 ...  85  87  82]]\n",
      "509\n",
      "359\n"
     ]
    }
   ],
   "source": [
    "# color split\n",
    "B, G, R = cv2.split(img)\n",
    "cv2.imshow('B', B)\n",
    "cv2.imshow('G', G)\n",
    "cv2.imshow('R', R)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n",
    "print(B)\n",
    "print(len(B))\n",
    "print(len(B[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#change color of image\n",
    "def random_light_color(img):\n",
    "    #brightness\n",
    "    B, G, R = cv2.split(img)\n",
    "    \n",
    "    b_rand = random.randint(-50, 50)\n",
    "    if b_rand == 0:\n",
    "        pass\n",
    "    elif b_rand > 0:\n",
    "        lim = 255 - b_rand\n",
    "        B[B > lim] = 255\n",
    "        B[B <= lim] = (b_rand + B[B <= lim]).astype(img.dtype)\n",
    "        \n",
    "    g_rand = random.randint(-50, 50)\n",
    "    if g_rand == 0:\n",
    "        pass\n",
    "    elif g_rand > 0:\n",
    "        lim = 255 - g_rand\n",
    "        G[G > lim] = 255\n",
    "        G[G <= lim] = (g_rand + G[G <= lim]).astype(img.dtype)  \n",
    "        \n",
    "    r_rand = random.randint(-50, 50)\n",
    "    if r_rand == 0:\n",
    "        pass\n",
    "    elif r_rand > 0:\n",
    "        lim = 255 - r_rand\n",
    "        R[R > lim] = 255\n",
    "        R[R <= lim] = (r_rand + R[R <= lim]).astype(img.dtype)    \n",
    "    img_merge = cv2.merge((B, G, R))\n",
    "    return img_merge\n",
    "img_random_color = random_light_color(img)\n",
    "cv2.imshow('img_random_color',img_random_color)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# gamma correction\n",
    "img_dark = cv2.imread('dark.png')\n",
    "cv2.imshow('img_dark', img_dark)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n",
    "def adjust_gamma(image, gamma = 1.0):\n",
    "    invGamma = 1.0 / gamma\n",
    "    table = []\n",
    "    for i in range(256):\n",
    "        table.append((((i / 255.0) ** invGamma) * 255))\n",
    "    table = np.array(table).astype(\"uint8\")\n",
    "    return cv2.LUT(img_dark, table)\n",
    "img_brighter = adjust_gamma(img_dark, 2)\n",
    "cv2.imshow('img_dark', img_dark)\n",
    "cv2.imshow('img_brighter', img_brighter)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAD8CAYAAACcjGjIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAFTxJREFUeJzt3X+MXeWd3/H3Z3HI0mxYTBgQsklNdq102UpL4ApcpYrapDWGVmtWChJRtbYiKldRskqkVi3p/mGS7B9JpW5apCwSXWjsKA1LsxthrWC9lpNq/wmEcUIghGU9+bFhaoq9MiG0kZKS/faP+zjczjM/7lwb7ozn/ZKuzjnf85xz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4wx/Brfl2oquer6utt/mXgGWAL5+HxXWasS5nKsd1I4bAFeG5keZ7lD8h6VcCfJznWPlEOcEVVPQ/DL0zg8qn17txbamzn8/H+ULuUcv/IJcLzZrxJtgHvAB7jPD++C8YKa+jYbqRwWOwP85yPj2q9s6quY/gXbj+Y5F3T7tCUnK/H+x7gV4BrgeeB/9jq58V4k/wS8MfAR6rqR8s1XaS2rsa7yFjX1LHdSOEw1p/oWO+q6kSbngS+xPD084Uzp9xtenJ6PTznlhrbeXm8q+qFqvpZVf0t8F949fLCuh9vkjcw/GH5+ar6k1Y+L4/vYmNda8d2I4XDef8nOpK8Kcmbz8wDO4FvMRzn3tZsL/DQdHr4mlhqbIeAPe2plh3AS2cuT6xnC66r/xbD4wvD8d6e5I1Jrga2A197vfs3qSQB7gOeqarfH1l13h3fpca65o7ttO/cv54vhk84/BXDu/2/O+3+vAbjexvDpxq+CTx9ZozAW4CjwPE2vXTafZ1wfF9geLr9fxn+NnXHUmNjeCr+mXasnwIG0+7/ORrv59p4nmT4Q+PKkfa/28b7LHDztPu/yrH+Q4aXSp4EnmivW87H47vMWNfUsfUT0pKkzka6rCRJGpPhIEnqGA6SpI7hIEnqGA6SpI7hIEnqGA6SpI7hIEnq/D9CimDesVfRiAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# histogram adjust\n",
    "img_small_brighter = cv2.resize(img_brighter, (int(img_brighter.shape[0]*0.5), int(img_brighter.shape[1]*0.5)))\n",
    "plt.hist(img_brighter.flatten(), 256, [0, 256], color = 'r')\n",
    "img_yuv = cv2.cvtColor(img_small_brighter, cv2.COLOR_BGR2YUV)\n",
    "# equalize the histogram of the Y channel\n",
    "img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])   # only for luminance(明亮度) channel\n",
    "# convert the YUV image back to RGB format\n",
    "img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)   # y: luminance(明亮度), u&v: 色度饱和度\n",
    "cv2.imshow('Color input image', img_small_brighter)\n",
    "cv2.imshow('Histogram equalized', img_output)\n",
    "plt.hist(img_output.flatten(), 256, [0, 256], color = 'g')\n",
    "key = cv2.waitKey(0)\n",
    "if key == 27:\n",
    "    exit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'shape'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-020b39136421>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mimg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'C:/Lenna.png'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mM\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetRotationMatrix2D\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m30\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mimg_rotate\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarpAffine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mM\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'rotated lenna'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mimg_rotate\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwaitKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'shape'"
     ]
    }
   ],
   "source": [
    "img = cv2.imread('C:/Lenna.png')\n",
    "M = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), 30, 1)\n",
    "img_rotate = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))\n",
    "cv2.imshow('rotated lenna', img_rotate)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n",
    "print (M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set M[0][2] = M[1][2] = 0\n",
    "M[0][2] = M[1][2] = 0\n",
    "print(M)\n",
    "img_rotate2 = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))\n",
    "cv2.imshow('rotated2 lenna', img_rotate2)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# scale+rotation+translation = similarity transform\n",
    "img = cv2.imread('Lenna.png')\n",
    "M = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), 30, 0.5 )#center angle scale\n",
    "img_rotated = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))\n",
    "cv2.imshow('rotated Lenna', img_rotated)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Affine Transform\n",
    "rows, cols, ch = img.shape\n",
    "pts1 = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]])\n",
    "pts2 = np.float32([[cols * 0.2, rows * 0.1 ], [cols * 0.9, rows * 0.2], [cols * 0.1, rows * 0.9]])\n",
    "\n",
    "M = cv2.getAffineTransform(pts1, pts2)\n",
    "dst = cv2.warpAffine(img, M, (cols, rows))\n",
    "\n",
    "cv2.imshow('affine lenna', dst)\n",
    "key = cv2.waitKey()\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# perspective transform\n",
    "def random_warp(img):\n",
    "    height, width, channels = img.shape\n",
    "\n",
    "    # warp:\n",
    "    random_margin = 60\n",
    "    x1 = random.randint(-random_margin, random_margin)\n",
    "    y1 = random.randint(-random_margin, random_margin)\n",
    "    x2 = random.randint(width - random_margin - 1, width - 1)\n",
    "    y2 = random.randint(-random_margin, random_margin)\n",
    "    x3 = random.randint(width - random_margin - 1, width - 1)\n",
    "    y3 = random.randint(height - random_margin - 1, height - 1)\n",
    "    x4 = random.randint(-random_margin, random_margin)\n",
    "    y4 = random.randint(height - random_margin - 1, height - 1)\n",
    "\n",
    "    dx1 = random.randint(-random_margin, random_margin)\n",
    "    dy1 = random.randint(-random_margin, random_margin)\n",
    "    dx2 = random.randint(width - random_margin - 1, width - 1)\n",
    "    dy2 = random.randint(-random_margin, random_margin)\n",
    "    dx3 = random.randint(width - random_margin - 1, width - 1)\n",
    "    dy3 = random.randint(height - random_margin - 1, height - 1)\n",
    "    dx4 = random.randint(-random_margin, random_margin)\n",
    "    dy4 = random.randint(height - random_margin - 1, height - 1)\n",
    "\n",
    "    pts1 = np.float32([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])#做投射变换需要4个点，原来的点\n",
    "    pts2 = np.float32([[dx1, dy1], [dx2, dy2], [dx3, dy3], [dx4, dy4]])#做投射变换需要4个点，目标点\n",
    "    M_warp = cv2.getPerspectiveTransform(pts1, pts2)\n",
    "    img_warp = cv2.warpPerspective(img, M_warp, (width, height))\n",
    "    return M_warp, img_warp\n",
    "\n",
    "\n",
    "M_warp, img_warp = random_warp(img)\n",
    "cv2.imshow('lenna_warp', img_warp)\n",
    "key = cv2.waitKey(0)\n",
    "if key == 27:\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
